package com.kkui.liuxin.controller;

import com.kkui.liuxin.service.MkdirService;
import com.kkui.liuxin.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.io.File;

/**
 * @ClassName: FileDelete
 * @Description: 提供删除文件和文件夹
 * @author: kkui
 * @date: 2022/4/13 23:37
 */


@Controller
@RequestMapping("/api")
public class FileDelete {

    @Resource
    MkdirService mkdirService;
    @Resource
    UserService userService;

    @ResponseBody
    @PostMapping("/delete")
    public String delete(@RequestParam("path") String path,@RequestHeader("Authorization") String authorization){


        File file = new File(mkdirService.getDefaultParentFolder() + userService.jwtGetUserId(authorization)
                + File.separator + path.replace("/",File.separator));
        if (file.exists()){
            mkdirService.deleteFile(file);
            return "删除成功";
        }else {
            return "删除目标不存在";
        }

    }
}
